using Microsoft.SmallBasic.Library;

namespace SmallBasicFun.Quizes
{
	public class HouseQuiz : IQuiz
	{
		private int small = 7;
		private  int medium  {get { return small*3; }}
		private  int big  {get { return medium*3; }}

		public Question Question1Small
		{
			get { return Quiz.questions[0]; }
		}

		public Question Question2Medium
		{
			get { return Quiz.questions[1]; }
		}

		public Question Question3Large
		{
			get { return Quiz.questions[2]; }
		}


		public Question Question4MoveTheLength
		{
			get { return Quiz.questions[3]; }
		}

		public Question Question5TurnTheCorner
		{
			get { return Quiz.questions[4]; }
		}

		public Question Question6DrawASide
		{
			get { return Quiz.questions[5]; }
		}

		public void StartTest()
		{
			Tortoise.InstantSpeed(true);
			Tortoise.SetPenWidth(3);
			GradeResults();
			InitializeScreen();
			DrawSuccess();
			Quiz.DisplayScores(6, 300);
		}

		private void DrawSuccess()
		{
			Tortoise.SetSpeed(10);
			ProgramWindow.SetBackgroundColor(Colors.Black);
			for (int k = 0; k < 6; k++)
			{
				triangle();
			}
		}

		private void triangle()
		{
			Tortoise.PenUp();
			Question2Medium.Run();
			Question3Large.Run();
			Tortoise.Move(Question3Large.GetVariable("length"));
			Tortoise.PenDown();
			Question5TurnTheCorner.Run();
			for (int i = 0; i < 2; i++)
			{
				Tortoise.SetPenWidth(3);
				Tortoise.SetPenColor(Colors.Lavender);
				Question3Large.Run();
				Question4MoveTheLength.Run();
				Tortoise.Turn(180);
				Question4MoveTheLength.Run();
				Tortoise.Turn(180);
				drawSmallerLine();
				Question5TurnTheCorner.Run();
			}
			Tortoise.PenUp();
			Question3Large.Run();
			Question4MoveTheLength.Run();
			Tortoise.PenDown();
			Tortoise.Turn(180);
			Question5TurnTheCorner.Run();
		}

		private void drawSmallerLine()
		{
			moveMedium();
			Tortoise.Turn(360/6);
			moveMedium();
			Question5TurnTheCorner.Run();
			moveMedium();
			Question5TurnTheCorner.Run();
			Tortoise.Turn(180);
			moveMedium();
		}

		private void moveMedium()
		{
			Question2Medium.Run();
			Tortoise.SetPenWidth(2);
			Tortoise.SetPenColor(Colors.LightBlue);
			Tortoise.SetPenWidth(2);
			Question4MoveTheLength.Run();
			Tortoise.Turn(180);
			Question4MoveTheLength.Run();
			Tortoise.Turn(180);
			drawSmallestLine();
		}

		private void drawSmallestLine()
		{
			Tortoise.SetPenWidth(1);
			Tortoise.SetPenColor(Colors.PowderBlue);
			Question1Small.Run();
			Question4MoveTheLength.Run();
			Tortoise.Turn(360/6);
			Question6DrawASide.Run();
			Question6DrawASide.Run();
			Tortoise.Turn(180);
			Question4MoveTheLength.Run();
		}


		private void InitializeScreen()
		{
			ProgramWindow.SetBackgroundColor(Colors.Black);
			Tortoise.SetPosition(100,75);
			Tortoise.SetAngle(90);
		}


		private void GradeResults()
		{
			GradeQuestion1Small();
			GradeQuestion2Medium();
			GradeQuestion3Large();
			GradeQuestion4MoveTheLength();
			GradeQuestion5TurnTheCorner();
			GradeQuestion6DrawASide();
		}

		private void GradeQuestion6DrawASide()
		{
			Question1Small.Run();
			TurtleGrader.GradeMoveTurtle(Question6DrawASide, small);
			if (Question6DrawASide.Result)
			{
				TurtleGrader.GradeTurnTurtle(Question6DrawASide, -360/3);
			}
		}

		private void GradeQuestion5TurnTheCorner()
		{
			TurtleGrader.GradeTurnTurtle(Question5TurnTheCorner, -360/3);
		}

		private void GradeQuestion4MoveTheLength()
		{
			Primitive l = 52;
			Question4MoveTheLength.SetVariable("length",l);
			TurtleGrader.GradeMoveTurtle(Question4MoveTheLength, l);
		}

		private void GradeQuestion3Large()
		{
			GradeLength(Question3Large, big);
		}

		private void GradeQuestion2Medium()
		{
			GradeLength(Question2Medium, medium);
		}

		private void GradeQuestion1Small()
		{
			GradeLength(Question1Small, small);
		}

		private void GradeLength(Question question, int expected)
		{
			question.Run();
			Primitive primitive = question.GetVariable("length");

			question.Result = primitive == expected;
		}
	}
}